//
//  ANGoodsInfoController.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/15.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANGoodsInfoController.h"
#import "AddToCartView.h"
#import "GoodsInfoTopView.h"
#import "ANSelectGoodsStandardsController.h"
#import "GoodsInfoEntity.h"
#import "GoodsStandardsEntity.h"
#import "ANOrderPayController.h"
#import "ANShopInfoController.h"
#import "ANIMController.h"

@interface ANGoodsInfoController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) GoodsInfoTopView *infoTopView;
@property (nonatomic, strong) UILabel *goodsInfoLab;


@property (nonatomic, strong) AddToCartView *addToCartView;

@property (nonatomic, strong) GoodsInfoTotalEntity *data;

@end

@implementation ANGoodsInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"商品详情";
    [self requestData];
}

- (void)requestData
{
    NSDictionary *para = @{
                           @"id":self.goodsEntity.goodsId
                           };
    [MBProgressHUD showActivityMessage:nil];
    [URLManager.shareManager getUrlStr:Api_Goods_Detail parameters:para entityClass:[GoodsInfoTotalEntity class] success:^(NSURLResponse * _Nonnull response, BaseEntity * _Nullable baseEntity, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (baseEntity) {
            if (baseEntity.code.intValue == 200) {
                
                self.data = baseEntity.data;
                NSString *htmlStr = self.data.goods.introduction;
                if (htmlStr && htmlStr.length > 0) {
                    self.infoTopView.showGoodsInfoTitle = YES;
                    [self showGoodsInfoWithHTMLString:htmlStr];
                }
                self.infoTopView.data = self.data;
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

- (void)showGoodsInfoWithHTMLString:(NSString *)HTMLString
{
    NSData *data = [HTMLString dataUsingEncoding:NSUnicodeStringEncoding];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithData:data options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    self.goodsInfoLab.attributedText = attri;
}

- (void)setupSubViews
{
    self.addToCartView = [[AddToCartView alloc] init];
    self.addToCartView.backgroundColor = kColorF2F2F2;
    [self.view addSubview:self.addToCartView];
    [self.addToCartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(55+kiPhoneXInsetBottom));
    }];
    WeakSelf
    self.addToCartView.contactHandler = ^{
        if (UserUtils.shared.isLogin) {
            ANIMController *vc = [[ANIMController alloc] init];
            vc.orgId = weakSelf.data.goods.consumer.orgId;
            vc.title = weakSelf.data.goods.consumer.orgName;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }
        else {
            [UserUtils showLoginVc];
        }
    };
    self.addToCartView.buyHandler = ^{
        if (UserUtils.shared.isLogin) {
            [weakSelf requestStandardsData:YES];
        }
        else {
            [UserUtils showLoginVc];
        }
    };
    self.addToCartView.addCartHandler = ^{
        if (UserUtils.shared.isLogin) {
            [weakSelf requestStandardsData:NO];
        }
        else {
            [UserUtils showLoginVc];
        }
    };
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.addToCartView.mas_top);
    }];
    
    self.infoTopView = [[GoodsInfoTopView alloc] init];
    [self.scrollView addSubview:self.infoTopView];
    [self.infoTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.scrollView);
    }];
    
    self.infoTopView.gotoConsumerPageHandle = ^{
        ANShopInfoController *vc = [[ANShopInfoController alloc] init];
        vc.orgId = weakSelf.data.goods.consumer.orgId;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    self.goodsInfoLab = [[UILabel alloc] init];
    self.goodsInfoLab.backgroundColor = [UIColor whiteColor];
    self.goodsInfoLab.numberOfLines = 0;
    [self.scrollView addSubview:self.goodsInfoLab];
    [self.goodsInfoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoTopView.mas_bottom);
        make.left.right.equalTo(self.scrollView);
        make.width.equalTo(@(kScreenW));
        make.bottom.equalTo(self.scrollView);
    }];
    

    
    
}

- (void)addWithData:(NSArray<GoodsStandardsSectionEntity *> *)data
{
    ANSelectGoodsStandardsController *vc = [[ANSelectGoodsStandardsController alloc] init];
    vc.dataSource = data;
    vc.sureHandler = ^(NSString * _Nonnull params, NSString * _Nonnull paramsName, int goodsNum) {
        [self addShopCart:params goodsNum:goodsNum];
    };
    [vc presentBottomViewControllerFromController:self];
}

- (void)buyData:(NSArray<GoodsStandardsSectionEntity *> *)data
{
    ANSelectGoodsStandardsController *vc = [[ANSelectGoodsStandardsController alloc] init];
    vc.dataSource = data;
    vc.sureHandler = ^(NSString * _Nonnull params, NSString * _Nonnull paramsName, int goodsNum) {
        ANOrderPayController *vc = [[ANOrderPayController alloc] init];
        vc.goodsInfoEntity = self.data;
        vc.paramsName = paramsName;
        vc.paramsId = params;
        vc.goodsNum = goodsNum;
        [self.navigationController pushViewController:vc animated:YES];
    };
    [vc presentBottomViewControllerFromController:self];
}

- (void)requestStandardsData:(BOOL)isBuy
{
    NSDictionary *body = @{
                           @"goodsId":self.data.goods.goodsId,
//                           @"goodsParam":@""
                           };
    [MBProgressHUD showActivityMessage:nil];
    [URLManager.shareManager getUrlStr:Api_Goods_Standards parameters:body entityClass:[GoodsStandardsSectionEntity class] success:^(NSURLResponse * _Nonnull response, BaseEntity * _Nullable baseEntity, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (baseEntity) {
            if (baseEntity.code.intValue == 200) {
                NSArray<GoodsStandardsSectionEntity *> *data = baseEntity.data;
                for (int i = 0; i < data.count; i++) {
                    GoodsStandardsSectionEntity *section = data[i];
                    for (int j = 0; j < section.params.count; j++) {
                        GoodsStandardsEntity *item = section.params[j];
                        if (j == 0) {
                            item.selected = @(YES);
                        }
                    }
                }
                if (isBuy) {
                    [self buyData:data];
                }
                else {
                    [self addWithData:data];
                }
            }
        }
        
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}


- (void)addShopCart:(NSString *)params goodsNum:(int)goodsNum
{
    NSDictionary *shoppingCart = @{
                                   @"goodsId":self.data.goods.goodsId,
                                   @"memberId":UserUtils.shared.vipId,
                                   @"purchaseNum":@(goodsNum)
                                   };
    NSString *shoppingCartJsonString = [shoppingCart yy_modelToJSONString];
    NSDictionary *body = @{
                           @"shoppingCart":shoppingCartJsonString,
                           @"params":params
                           };
    [MBProgressHUD showActivityMessage:nil];
    [URLManager.shareManager postUrlStr:Api_Add_ShoppingCart parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            [MBProgressHUD showMessage:responseObj[@"message"]];
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
